package com.pro01.backend.mapper;

import org.apache.ibatis.annotations.*;

@Mapper
public interface Forum2PostLikeMapper {

    // 点赞
    @Insert("""
        INSERT INTO forum2_post_likes (user_id, post_id)
        VALUES (#{userId}, #{postId})
    """)
    int insertLike(@Param("userId") Long userId, @Param("postId") Long postId);

    @Select("""
        SELECT COUNT(*) FROM forum2_post_likes
        WHERE user_id = #{userId} AND post_id = #{postId}
    """)
    boolean hasLiked(@Param("userId") Long userId, @Param("postId") Long postId);

    @Update("""
        UPDATE forum2_posts
        SET like_count = like_count + 1
        WHERE id = #{postId}
    """)
    int incrementLikeCount(@Param("postId") Long postId);

    // 取消点赞
    @Delete("""
    DELETE FROM forum2_post_likes
    WHERE user_id = #{userId} AND post_id = #{postId}
""")
    int deleteLike(@Param("userId") Long userId, @Param("postId") Long postId);

    @Update("""
    UPDATE forum2_posts
    SET like_count = GREATEST(like_count - 1, 0)
    WHERE id = #{postId}
""")
    int decrementLikeCount(@Param("postId") Long postId);

    // 检查是否点赞
    @Select("""
    SELECT COUNT(*) > 0
    FROM forum2_post_likes
    WHERE user_id = #{userId} AND post_id = #{postId}
""")
    boolean hasUserLiked(@Param("userId") Long userId, @Param("postId") Long postId);

}

